﻿using System;
using System.Collections.Generic;
using Mitosoft.Definitions.Data;
using TextFileReaderModule.Interfaces;
using TextFileReaderModule.ViewModels;

namespace TextFileReaderModule.Manager
{
  /// <summary>
  /// Manager responsible to handle creating and storing <see cref="GenericDataBlockItemViewModel"/> instances.
  /// </summary>
  public class GenericDataBlockItemViewModelManager
  {
    #region fields

    /// <summary>
    /// Represents the service handling comments.
    /// </summary>
    protected ICommentService _commentService;

    /// <summary>
    /// Contains all created view models.
    /// </summary>
    protected Dictionary<ulong, GenericDataBlockItemViewModel> 
      _genericDataBlockItemViewModelList = new Dictionary<ulong, GenericDataBlockItemViewModel>();

    #endregion

    #region constructor(s)

    /// <summary>
    /// Creates a new <see cref="GenericDataBlockItemViewModelManager"/> instance.
    /// </summary>
    /// <param name="commentService">Represents the service handling the comments.</param>
    public GenericDataBlockItemViewModelManager(ICommentService commentService)
    {
      if (commentService == null)
        throw new ArgumentNullException("commentService");

      _commentService = commentService;
    }

    #endregion

    #region properties

    #endregion

    #region methods

    /// <summary>
    /// Gets a new or a previous generated <see cref="GenericDataBlockItemViewModel"/> instance.
    /// </summary>
    /// <param name="source">Data representing the model from which we want the correlated view model.</param>
    /// <param name="sourceIndex">Represents the source index.</param>
    /// <returns>Found or generated view model.</returns>
    public IGenericDataBlockItem<string> GetGenericDataBlockItemViewModel(
      IGenericDataBlockItem<string> source, int sourceIndex)
    {
      if (source == null)
        throw new ArgumentNullException("source");

      if (_genericDataBlockItemViewModelList.ContainsKey(source.Id))
        return _genericDataBlockItemViewModelList[source.Id];

      return 
        GenericDataBlockItemViewModel.Instance(
          source,
          sourceIndex,
          _commentService,
          item =>
          {
            // Action is initiated when "specific" properties of the 
            // generated view model changes. Then we want to store internally
            // the view model to return it when it is requested later.

            if (item == null) return;

            // Add item if it is not already present:
            if (! _genericDataBlockItemViewModelList.ContainsKey(item.Id))
              _genericDataBlockItemViewModelList.Add(item.Id, item);
          }
        );
    }

    /// <summary>
    /// Clears internally stored view models.
    /// </summary>
    public void Clear()
    {
      _genericDataBlockItemViewModelList.Clear();
    }

    #endregion
  }
}
